/***************************************************************************
*@Author: Nic Wu
*@Created on 20/04/2011
*@Class Name:  ForecastController
*@Main Methods: Save(), Submit(), Cancel()
*
*@Useage: 
*it used to retrieve the forecast budgets in RMB & KG from CityBudget obj, 
*then stored all of them into SKUForecast & MonthlyForecast obj.
*after this, display the data on ForecastPage.
*
****************************************************************************/

public with sharing class ForecastController {

    public String errMsg{get;set;}
    public List<CityMonthlyBudget__c> CityMonBudgets{get;set;}
    public RollingForecast__c r{get; set;}
    public Decimal TotalBudgetRMB{get;set;}
    public Decimal TotalBudgetKG{get;set;}
    public List<MonthlyForecast__c> insertMonths =new List<MonthlyForecast__c>();
    public List<MonthlyForecast__c> updateMonths = new List<MonthlyForecast__c>();
    public List<MonthlyForecast__c> MRecList {get;set;}
    public List<Product2> CIList{get;set;}
    public boolean isRendered {get;set;}
    public boolean disableInputs {get;set;}
    public String Status{get;set;}
    public String percentage{get;set;}
    public boolean InsertOrUpdate;
    public String CityName{get;set;}
    public List<Product2> Inven{get;set;}
    public List<MonthlyForecast__c> ExistedMonthsRec =new List<MonthlyForecast__c>();

    public Decimal TotalForecastRMB{get;set;}
    public Decimal TotalForecastKG{get;set;}
    public Date CurrentDate{get;set;}
    public String ErrDetail{get;set;}
    public String MsgType{get;set;}
    public List<String> ProductNames = new List<String>();
    String pro1;
    String pro2;
    String pro3;
    String pro4;
    String pro5;
    String pro6;
    String pro7;
    public List<CheckInventory__c> checkInvenList{get;set;}
    public Boolean DisplayApprove{get;set;}
    public Boolean DisplayReject{get;set;}
    public Boolean DisplaySubmit{get;set;}
    public Boolean DisplaySave{get;set;}
    public Boolean DisplayAllocate{get;set;}
    public Boolean DisplayTotal{get;set;}
    public Boolean DisplayEdit{get;set;}
    public Boolean DisplayRM{get;set;}
    
    public String RollingForecastID{get;set;}

    public List<Place__c> pList = new  List<Place__c>();  
    User cu  = [Select Name,UserRoleId,ProfileId,Title__c,email,ManagerId From User Where Id =:UserInfo.getUserId()];
    //UserRole ur = [Select Id,Name,ParentRoleId  From UserRole Where Id =: cu.UserRoleId]; 
    
    //角色
    public UserRole RoleNMKD = [Select u.Name, u.Id From UserRole u Where u.Name =: GlobalProperties.RoleNMKD ];
    public UserRole RoleNMSD = [Select u.Name, u.Id From UserRole u Where u.Name =: GlobalProperties.RoleNMSD ];
    public UserRole RoleNMMKTM = [Select u.Name, u.Id From UserRole u Where u.Name =: GlobalProperties.RoleNMMKTM ];
    
    //简档
    public Profile ProfileRSM= [Select p.Name, p.Id From Profile p Where p.Name =: GlobalProperties.ProfileRSM_M];
    public Profile ProfileRSM_TM = [Select p.Name, p.Id From Profile p Where p.Name =: GlobalProperties.ProfileRSM_TM];
    public Profile ProfileRGM= [Select p.Name, p.Id From Profile p Where p.Name =: GlobalProperties.ProfileRGM];
    public Profile ProfileNMMM = [Select p.Name, p.Id From Profile p Where p.Name =: GlobalProperties.ProfileNMMM ];
    public Profile ProfileRMMM = [Select p.Name, p.Id From Profile p Where p.Name =: GlobalProperties.ProfileRMMM ];
    public Profile ProfileSRMMM = [Select p.Name, p.Id From Profile p Where p.Name =: GlobalProperties.ProfileSRMMM ];

    /*
        如果monthly forecast 里面没有数据 则插入 13条记录
        如果monthly forecast 里面存在数据, 则根据里面的年月判断插入多少条记录
        整合所有monthly forecast的数据到一个 data class 里面, 然后再insert
    **/
    public List<MonthlyData> CommonList{get;set;}
    public List<MonthlyData> ExistedRec{get;set;}
    public List<MonthlyData> ERec = new List<MonthlyData>();
    public String SaveVal{get;set;}

    //constructor
    public ForecastController(){

        String cidHeader = '?CityId=';
        String str = System.currentPageReference().getParameters().get('rid');
        String rid = str.substring(0,str.indexOf(cidHeader));
        RollingForecastID = rid;
       
        DisplaySubmit = true;
        DisplaySave = true;
        //插入7种预测产品
        Pro1 = '安婴儿A+无乳糖400克罐装（样品）';
        Pro2 = '安婴儿A+200克盒装（样品）';
        Pro3 = '安婴儿A+400克罐装（样品）';
        Pro4 = '安婴儿A+早产儿配方奶粉400克罐装（样品）';
        Pro5 = '安婴儿A+早产儿配方奶59毫升×6瓶装（样品）';
        Pro6 = '美赞臣无苯丙氨酸配方粉（1阶段）454克罐装';
        Pro7 = '安婴乐450克罐装（样品）'; 

        //加入到pname list
        ProductNames.add(Pro1);
        ProductNames.add(Pro2);
        ProductNames.add(Pro3);
        ProductNames.add(Pro4);
        ProductNames.add(Pro5);
        ProductNames.add(Pro6);
        ProductNames.add(Pro7);

        //Date.today();
        CurrentDate = Date.today();
        try {
            List<CityBudget__c> CBList = new List<CityBudget__c>();
            CBList = [Select Id, Name, Month__c, City__c, Rate__c From CityBudget__c limit 1];
            if(CBList.size() > 0)
                CityBudget = [Select Id, Name, Month__c, City__c, Rate__c From CityBudget__c limit 1];
        } catch (QueryException e) {
                Rate = 0;
        }

        //City inventory list
        CIList = [select id, name, Product_Unit_Volume__c from Product2 where Name in:ProductNames and Product_Type__c =: 'PE Sample'];
        CIListCount = CIList.size();
        //Monthly record list
        MRecList = [select id, name, year__c, month__c, BudgetKG__c, BudgetRMB__c from
                          MonthlyForecast__c where year__c=:CurrentDate.year() and month__c<: CurrentDate.month() 
                          and Rolling_Forcast__c =: RollingForecastID];
        //get city name
        CityName = [select id, name from Place__c where id =: CityId limit 1][0].name;
         
         for(Integer i=1;i<Date.today().month()+2;i++){
            List<MonthlyForecast__c> m = [select id, name, year__c, month__c, Theory_Inventory__c, Rolling_Forcast__c, City_Monthly_Budget__c,
                                                                     BudgetKG__c, BudgetRMB__c, City__c, Rate__c, TotalKG__c, TotalRMB__c from MonthlyForecast__c 
                                                                      where City__c =: CityName and year__c=: Date.today().year() and  month__c =:i order by CreatedDate desc limit 1]; 
            if(m.size()>0){
                ExistedMonthlyForecastList.add(m[0]);
            }
        }
        //获取city months budgets
        CityMonBudgets = [select BudgetRMB__c, BudgetKG__c, Month__c, Year__c, Rate__c, RECORDTYPE__C from 
                                           CityMonthlyBudget__c where  RECORDTYPE__C='Budget' and  SalesCity__c =:CityName]; 
        //Existed monthly records
        ExistedMonthsRec = [select id, name, Month__c, Year__c, City__c, Theory_Inventory__c, BudgetKG__c, BudgetRMB__c from 
                                               MonthlyForecast__c where Rolling_Forcast__c=: RollingForecastID];
       r = [select r.Year__c, r.UserEmail__c, r.UniqueId__c, r.TotalForecastRMB__c, r.TotalForecastKG__c, Rate__c,
               r.TotalBudgetRMB__c, r.TotalBudgetKG__c,  r.Sales_City__c,r.RMMM__c ,
               r.OwnerId, r.Name,  r.Month__c,  r.Id, 
               r.ForecastCity__c, r.Approver__c, r.ApprovalStatus__c, r.Apply_Date__c from RollingForecast__c r where r.Id =: RollingForecastID limit 1] ;                                      
      Status = r.ApprovalStatus__c;

      List<MonthlyForecast__c> mf = [select id, name from MonthlyForecast__c where Rolling_Forcast__c =: rid];

        if(mf.size()>0){
            InsertOrUpdate = false;
            isRendered = true;
            TotalForecast();
            initExistedRec();
            displayRecords();
        }else{
            InsertOrUpdate = true;
            isRendered = true;
            TotalForecast();
            initInventory();
            initExistedRec();
            initEmptyRec();
        }

        if(InsertOrUpdate){
            SaveVal = '保存';
        }else{
            SaveVal = '修改';
        }
        DisplayRM = true;

       if(r.ApprovalStatus__c == 'Waiting for RMMM Approve')
       {
            if(cu.ProfileId == ProfileRMMM.id)
            {
                DisplayApprove = true;
                DisplayReject = true;
                DisplayEdit = false;
                DisplaySave = false; 
                DisplaySubmit = false;
                DisplayRM = false;
                disableInputs =false;
            }
            else
            {
                DisplayApprove = false;
                DisplayReject = false;
                DisplayEdit = false;
                DisplaySave = false;  
                DisplaySubmit = false;
                DisplayRM = false;
                disableInputs =true;
            }
            DisplayRecords();
       }
       else if(r.ApprovalStatus__c == 'Waiting for RSM Approve')
       {
            if((cu.ProfileId == ProfileRSM.id)||(cu.ProfileId == ProfileRSM_TM.id))
            {
                DisplayApprove = true;
                DisplayReject = false;
                DisplayEdit = true;
                DisplaySave = true; 
                DisplaySubmit = false;
                DisplayRM = false;
                disableInputs =false;
            }
            else
            {
                DisplayApprove = false;
                DisplayReject = false;
                DisplayEdit = false;
                DisplaySave = false;      
                DisplaySubmit = false;
                disableInputs =true;
            }
            DisplayRecords();
       }
       else if(r.ApprovalStatus__c == 'Waiting for SRMMM Approve')
       {
            if(cu.ProfileId == ProfileSRMMM.id)
            {
                DisplayApprove = true;
                DisplayReject = true;
                DisplayEdit = false;
                DisplaySave = false;    
                DisplaySubmit = false;
                DisplayRM = false;
                disableInputs =false;
            }
            else
            {
                DisplayApprove = false;
                DisplayReject = false;
                DisplayEdit = false;
                DisplaySave = false;    
                DisplaySubmit = false;
                disableInputs =true;             
            }
            DisplayRecords();
       }
       else if(r.ApprovalStatus__c == 'Waiting for National Medical MKT Manager Approve')
       {
            if(cu.ProfileId == ProfileNMMM.id)
            {
                DisplayApprove = true;
                DisplayReject = false;
                DisplayEdit = true;
                DisplaySave = true;     
                DisplaySubmit = false;
                DisplayRM = false;
                disableInputs =false;
            }
            else
            {
                DisplayApprove = false;
                DisplayReject = false;
                DisplayEdit = false;
                DisplaySave = false;      
                DisplaySubmit = false;
                disableInputs =true;        
            }
            DisplayRecords();
       }
       else if(r.ApprovalStatus__c == 'Waiting for Medical MKT Director Approve')
       {
            if(cu.UserRoleId == RoleNMKD.id)
            {
                DisplayApprove = true;
                DisplayReject = false;
                DisplayEdit = true;
                DisplaySave = true;      
                DisplaySubmit = false;
                DisplayRM = false;
                disableInputs =false;
            }
            else
            {
                DisplayApprove = false;
                DisplayReject = false;
                DisplayEdit = false;
                DisplaySave = false;         
                DisplaySubmit = false;
                disableInputs =true;         
            }
            DisplayRecords();
       }
       else if(r.ApprovalStatus__c == 'Waiting for National Medical Sales Director Approve')
       {
            if(cu.UserRoleId == RoleNMSD.id)
            {
                DisplayApprove = true;
                DisplayReject = false;
                DisplayEdit = true;
                DisplaySave = true;        
                DisplaySubmit = false;
                DisplayRM = false;   
                disableInputs =false;
            }
            else
            {
                DisplayApprove = false;
                DisplayReject = false;
                DisplayEdit = false;
                DisplaySave = false;    
                DisplaySubmit = false;
                disableInputs =true;               
            }
            DisplayRecords();
       }
       else if(r.ApprovalStatus__c == 'Approve')
       {
           DisplayApprove = false;
           DisplayReject = false;
           DisplayEdit = false;
           DisplaySave = false;
           DisplaySubmit = false;  
           disableInputs =true;
       } 
       //DisplayRecords();
    }

    //显示给上级审批的时候看的List
    public void DisplayRecords(){
    
            commonList = new List<MonthlyData>();
            RollingForecast__c rr = [select id, name, Apply_Date__c, ApprovalStatus__c, ForecastCity__c, Month__c, TotalBudgetKG__c,  TotalBudgetRMB__c, 
                                              TotalForecastKG__c, TotalForecastRMB__c,UniqueId__c,UserEmail__c,Year__c from RollingForecast__c where id =:RollingForecastID limit 1];
            
            TotalBudgetRMB = rr.TotalBudgetRMB__c;
            TotalBudgetKG = rr.TotalBudgetKG__c;
            TotalForecastRMB= rr.TotalForecastRMB__c;
            TotalForecastKG = rr.TotalForecastKG__c;
            
            List<MonthlyForecast__c> mList= [select id, name,BudgetKG__c,BudgetRMB__c,City_Monthly_Budget__c, Year__c, Month__c,Rate__c,Theory_Inventory__c,
                                                                      TotalKG__c, TotalRMB__c from MonthlyForecast__c where Rolling_Forcast__c=:rr.id order by  year__c , month__c asc];
            
            List<String> strList = new List<String>();
            for(Integer i=0;i<mList.size();i++){
                strList.add(mList[i].id);
            }
            
            for(Integer i=0; i<mList.size();i++){
            
                MonthlyData md = new MonthlyData();
                md.caseList = new List<Integer>();
                md.RollingForecastID = rr.id;
                md.cityNam =rr.name;
                md.Year =Integer.valueOf(mList[i].year__c);
                md.month = Integer.valueOf(mList[i].month__c);
                
                if(mList[i].BudgetRMB__c!=null){
                    md.budgetRMB =mList[i].BudgetRMB__c;
                }else{
                    md.budgetRMB =0.0;
                }
                //md.budgetRMB =mList[i].BudgetRMB__c;
                if(mList[i].BudgetKG__c!=null){
                    md.budgetKG =mList[i].BudgetKG__c;
                }else{
                    md.budgetKG=0.0;
                }
                //md.budgetKG = mList[i].BudgetKG__c;
                md.Rate = mList[i].Rate__c;
                md.TotalRMB=mList[i].TotalRMB__c;
                md.TotalKG=mList[i].TotalKG__c;
                
                List<SKU__c> sList = [select id, name, Case__c, KG__c, Month__c, RMB__c, SKU_Name__c, product__c,MonthlyForecast__c 
                                                       from SKU__c where MonthlyForecast__c =:mList[i].id];

                for(Integer j=0;j<sList.size();j++) {
                    for(Integer k=0;k<CIList.size();k++){
                        if(sList[j].MonthlyForecast__c==mList[i].id && sList[j].product__c == CIList[k].id){
                            md.CaseList.add(Integer.valueOf(sList[j].Case__c));
                            if(k==0){
                                md.Resource1=Integer.valueOf(sList[j].Case__c);
                            }else if(k==1){
                                md.Resource2=Integer.valueOf(sList[j].Case__c);
                            }else if(k==2){
                                md.Resource3=Integer.valueOf(sList[j].Case__c);
                            }else if(k==3){
                                md.Resource4=Integer.valueOf(sList[j].Case__c);
                            }else if(k==4){
                                md.Resource5=Integer.valueOf(sList[j].Case__c);
                            }else if(k==5){
                                md.Resource6=Integer.valueOf(sList[j].Case__c);
                            }else if(k==6){
                                md.Resource7=Integer.valueOf(sList[j].Case__c);
                            }
                        }
                    }
                }
            commonList.add(md);
        }
    }
    
    public Integer CIListCount{get;set;}
    Public List<MonthlyData> Rec{
        get{ //&& ExistedRec[i].month < Date.today().month()+1
            for(Integer i=0;i<ExistedRec.size();i++){
                if(ExistedRec[i].year == CurrentDate.year())
                ERec.add(ExistedRec[i]);
            }
            return ERec;
        }
        set;
    }

    public Double Rate{
        get{
                return r.Rate__c;
        }
        set;
    }
    
    public CityBudget__c CityBudget{
         get{
             if(CityBudget== null)
                 CityBudget = new CityBudget__c();
             return CityBudget;
         }
         set;
    }

    public List<MonthlyForecast__c> ExistedMonthlyForecastList{
        get{
            if(ExistedMonthlyForecastList==null)
                ExistedMonthlyForecastList = new List<MonthlyForecast__c>();
             return ExistedMonthlyForecastList;
        }
        set;
    }
    
    public void TotalForecast(){
       
       if(r!=null ){
           if(r.TotalForecastRMB__c !=null && r.TotalForecastKG__c!=null){
               TotalForecastRMB = r.TotalForecastRMB__c;
               TotalForecastKG = r.TotalForecastKG__c;
           }
       }
    }
    
    public List<String>getProductNames(){
        return ProductNames;
    }
    
    //initialize inventory list
    public void initInventory() {
          List<String> idList = new List<String>();
           for(Integer i=0;i<CIList.size();i++){
                idList.add(CIList[i].id);
           }
           Inven =[select id, name,Product_Unit_Volume__c from Product2 where id in: idList];
    }
    
    public void initExistedRec() {
        //initialize the MonthlyData list
        if(ExistedRec == null)
            ExistedRec = new List<MonthlyData>();
        else
            ExistedRec.clear();
            
            List<MonthlyForecast__c> mList = ExistedMonthlyForecastList;
          
        //已存在的sku records
        List<SKU__c> sList =[select id, name, Month__c, year__c, MonthlyForecast__c, SKU_Name__c, Case__c, Product__c, Approver__c,Rolling_Forcast__c 
                                              from SKU__c where MonthlyForecast__c in: mList];
        if(sList!=null && sList.size()<1){
            return;
        }

        /*
            确定要获取多少条monthly forecast 的记录
            根据需求, 这个table 需要显示今年以内, 到要预测的那个月的 records
        **/
        
       for(Integer i=0; i<mList.size();i++){
            MonthlyData md = new MonthlyData();
            md.caseList = new List<Integer>();
            md.RollingForecastID = RollingForecastID;
            md.cityNam = CityName;
            md.Year =Integer.valueOf(mList[i].year__c);
            md.month = Integer.valueOf(mList[i].month__c);
            md.budgetRMB =mList[i].BudgetRMB__c;
            md.budgetKG = mList[i].BudgetKG__c;
            md.Rate = mList[i].Rate__c;
            md.TotalRMB=mList[i].TotalRMB__c;
            md.TotalKG=mList[i].TotalKG__c;

            for(Integer j=0;j<sList.size();j++) {
                for(Integer k=0;k<CIList.size();k++){
                    if(sList[j].MonthlyForecast__c==mList[i].id && sList[j].product__c == CIList[k].id){
                        md.CaseList.add(Integer.valueOf(sList[j].Case__c));
                    }
                }
            }
            ExistedRec.add(md);
        }
        
        if(ExistedMonthlyForecastList.size()>0){
            hasExistedRec =true;
        }else{
            hasExistedRec =false;
        }
       
    }
    
    public void initExistedRec1(){
    
            List<MonthlyForecast__c> mList = new List<MonthlyForecast__c>();
            List<MonthlyForecast__c> m  = [select id, name, year__c, month__c, Theory_Inventory__c, Rolling_Forcast__c, City_Monthly_Budget__c,
                                                                             BudgetKG__c, BudgetRMB__c, City__c, Rate__c, TotalKG__c, TotalRMB__c from MonthlyForecast__c 
                                                                             where City__c =:CityName and Rolling_Forcast__r.year__c =:String.valueOf(Date.today().year())
                                                                             and Rolling_Forcast__r.month__c =:String.valueOf(Date.today().month()-1) ];
            
            for(Integer i=0;i<12;i++){
                Date dat = CurrentDate.addMonths(i+2);               
                for(Integer j=0;j<m.size();j++){
                    if(m[j].year__c ==dat.year() && m[j].month__c == dat.month()){
                        mList.add(m[j]);
                    }
                }
            }
            
            //已存在的sku records
            List<SKU__c> sList =[select id, name, Month__c, year__c, MonthlyForecast__c, SKU_Name__c, Case__c, Product__c, Approver__c,Rolling_Forcast__c 
                                                  from SKU__c where MonthlyForecast__c in: mList];
            if(sList.size()>0){
                    /*
                        确定要获取多少条monthly forecast 的记录
                        根据需求, 这个table 需要显示今年以内, 到要预测的那个月的 records
                    **/
                    
                    for(Integer i=0; i<mList.size();i++){
                        MonthlyData md = new MonthlyData();
                        md.caseList = new List<Integer>();
                        md.RollingForecastID = RollingForecastID;
                        md.cityNam =CityName;
                        md.Year =Integer.valueOf(mList[i].year__c);
                        md.month = Integer.valueOf(mList[i].month__c);

                        if(mList[i].BudgetRMB__c!=null){
                            md.budgetRMB =mList[i].BudgetRMB__c;
                        }else{
                            md.budgetRMB =0.0;
                        }
                        
                        if(mList[i].BudgetKG__c!=null){
                            md.budgetKG =mList[i].BudgetKG__c;
                        }else{
                            md.budgetKG=0.0;
                        }

                        md.Rate = mList[i].Rate__c;
                        md.TotalRMB=mList[i].TotalRMB__c;
                        md.TotalKG=mList[i].TotalKG__c;
        
                        for(Integer j=0;j<sList.size();j++) {
                            for(Integer k=0;k<CIList.size();k++){
                                if(sList[j].MonthlyForecast__c==mList[i].id && sList[j].product__c == CIList[k].id){
                                    md.CaseList.add(Integer.valueOf(sList[j].Case__c));
                                    if(k==0){
                                        md.Resource1=Integer.valueOf(sList[j].Case__c);
                                    }else if(k==1){
                                        md.Resource2=Integer.valueOf(sList[j].Case__c);
                                    }else if(k==2){
                                        md.Resource3=Integer.valueOf(sList[j].Case__c);
                                    }else if(k==3){
                                        md.Resource4=Integer.valueOf(sList[j].Case__c);
                                    }else if(k==4){
                                        md.Resource5=Integer.valueOf(sList[j].Case__c);
                                    }else if(k==5){
                                        md.Resource6=Integer.valueOf(sList[j].Case__c);
                                    }else if(k==6){
                                        md.Resource7=Integer.valueOf(sList[j].Case__c);
                                    }
                                }
                            }
                        }
                    ExistedRec1.add(md);
                }                    
            }
    }
    public boolean hasExistedRec{get;set;}
    public List<MonthlyData> ExistedRec1 {
    
        get{
            //initialize the MonthlyData list
            if(ExistedRec1 == null)
                ExistedRec1 = new List<MonthlyData>();

            return ExistedRec1;
        }
        set;
    }

    public List<MonthlyData> EmptyRec{get;set;}
    public List<MonthlyData> EditableRec{get;set;}
    
    public void initEmptyRec(){
        if(EmptyRec==null)
            EmptyRec = new List<MonthlyData>();
        else
            EmptyRec.clear();
        if(EditableRec == null)
            EditableRec=new List<MonthlyData>();
        else
            EditableRec.clear();

        Integer count=0;
        Integer index=0;
        initExistedRec1();
        
        for(Integer i=0;i<ExistedRec1.size();i++){
            Date dat = CurrentDate.addMonths(count+2);
            if(count+2<14 && ExistedRec1[i].month==dat.month() && ExistedRec1[i].year==dat.year()){
                EditableRec.add(ExistedRec1[i]);
            }
            count++;
        }

        Integer addCount=0;
        if(EditableRec.size()<12){
            addCount = 12 - EditableRec.size();
        }
        initCommonList(addCount);
    }

    public void initCommonList(Integer index){
        TotalBudgetRMB =0.0;
        TotalBudgetKG = 0.0;
        
        if(CommonList==NULL)
               CommonList = new List<MonthlyData>();
        for(Integer i=0;i<ExistedRec1.size();i++){
            CommonList.add(ExistedRec1[i]);
        }
        
        for(Integer i=0;i<index;i++) {
            Date dat = CurrentDate.addMonths(EditableRec.size()+2+i);
            MonthlyData cde = new MonthlyData();
            cde.year =dat.year();
            cde.month =dat.month();
            cde.RollingForecastID = RollingForecastID;

            if(String.valueOf(cde.month).length()<2){
                    cde.Name = dat.year()+'年0'+dat.month()+'月预测';
            }else{
                cde.Name = dat.year()+'年'+dat.month()+'月预测';
            }

            cde.CityNam = CityName;
            cde.budgetRMB=0.0;
            cde.budgetKG=0.0;
            cde.TotalRMB=0.0;
            cde.TotalKG =0.0;
            cde.Rate = Rate;
            for(Integer j=0;j<CityMonBudgets.size();j++){
                   if(CityMonBudgets[j].month__c == string.valueOf(cde.month) && CityMonBudgets[j].year__c == string.valueOf(cde.year)) {
                       cde.budgetRMB += CityMonBudgets[j].BudgetRMB__c;
                       cde.budgetKG += CityMonBudgets[j].BudgetKG__c;
                   }
            }
            if(cde.budgetRMB ==null){
                cde.budgetRMB =0;
            }
             if(cde.budgetKG ==null){
                cde.budgetKG =0;
            }
             CommonList.add(cde);
        }
        if(commonList.size()>0 && commonList !=null){
            for(Integer i=0;i<CommonList.size();i++){
                if(CommonList[i].budgetRMB!=null && CommonList[i].budgetKG!=null){
                    TotalBudgetRMB+=CommonList[i].budgetRMB;
                    TotalBudgetKG += CommonList[i].budgetKG;
                }
            }
        }
    }
    
    
    public String CityId{
        get{
            String cidHeader = '?CityId=';
            String str = System.currentPageReference().getParameters().get('rid');
            String rid = str.substring(str.length()-str.indexOf(cidHeader), str.length());
            return rid;
        }
        set;
    }
    
    public void MonBudgets(MonthlyForecast__c cde, Integer mon, Integer yr){
            cde.City_Monthly_Budget__c=0.0;
            
            for(Integer i=0;i<CityMonBudgets.size();i++){
                if(CityMonBudgets[i].month__c == string.valueOf(mon) && CityMonBudgets[i].year__c == string.valueOf(yr)) {
                    cde.Rate__c = Rate;
                    cde.City_Monthly_Budget__c += CityMonBudgets[i].BudgetRMB__c;
                }
            }
    }
    
    //获取当前登录的user id
    public id UsrID{
        get{
        id uid =UserInfo.getUserId();
        return uid;
        }
        set;
    }
    
     public  boolean isR{
        get{
        if(isRendered)
            return false;
        else
            return true;
        }
        set;
    }
    
    /*
        获取 URL的id
    **/
    public String UrlId{
        get{
                return System.currentPageReference().getParameters().get('id');
        }
        set;
    }
    public boolean isShow = false;
    public boolean MsgShow{
        get{
            MsgShow = isShow;
            return MsgShow;
        }
        set;
    }
    
    public void updateMonthsList(){
        //插入的时候, 判断 是否已经插入了的, 如果true, 则不能再插入
        if(InsertOrUpdate){
            isShow = true;
            MsgType='WARNING';
            errMsg ='不能更新, 只能插入预测数据';
            SaveVal = '保存';
            InsertOrUpdate = false;
            return;
        }
        
        /**
            要update, 首先要获取monthlyforecast 的id
        */
        Savepoint sp=DataBase.setSavepoint();
        updateMonths = new List<MonthlyForecast__c>();        
        List<MonthlyForecast__c> lm = [select id, name,year__c,month__c,Rate__c,City_Monthly_Budget__c from MonthlyForecast__c where Rolling_Forcast__c =: RollingForecastID];
       
        if(lm.size()>0){
       
            for(Integer i=0;i<CommonList.size();i++){
                Date dat = CurrentDate.addMonths(i+2);
                //lm[i].Rolling_Forcast__c = CommonList[i].RollingForecastID;
                lm[i].year__c = CommonList[i].Year;
                lm[i].month__c = CommonList[i].month;
                
                if(String.valueOf(lm[i].month__c).length()<2){
                    lm[i].Name = dat.year()+'年0'+dat.month()+'月预测';
                }else{
                    lm[i].Name = dat.year()+'年'+dat.month()+'月预测';
                }
                
                lm[i].Rate__c = CommonList[i].Rate;
                lm[i].City_Monthly_Budget__c = CommonList[i].budgetRMB;
                updateMonths.add(lm[i]);
            }
             update updateMonths;
        }
        
        /**
           将修改过的sku 的case 写入到 caseList list
       */
        List<SKU__c> skuList = new List<SKU__c>();
        List<Integer> caseList = new List<Integer>();
        
        for(Integer i=0;i<CommonList.size();i++){
            if(CIListCount>1)
                  caseList.add(CommonList[i].Resource1);
            if(CIListCount>2)
                  caseList.add(CommonList[i].Resource2);
            if(CIListCount>=3)
                  caseList.add(CommonList[i].Resource3);
            if(CIListCount>=4)
                  caseList.add(CommonList[i].Resource4);
            if(CIListCount>=5)
                  caseList.add(CommonList[i].Resource5);
            if(CIListCount>=6)
                  caseList.add(CommonList[i].Resource6);
            if(CIListCount>=7)
                  caseList.add(CommonList[i].Resource7);
        }
        
        /**
            首先创建一个 nested list 去装每个月的sku records,
            通过循环caseList, 每7个sku records装在一个list 里面,
            然后再装到 nested list
            
            这个nested list 为了就是要在下面的 insertion中, 
            一次性的 insert 所有的 skuList 到每个月的record 里面
        */
        List<List<Integer>> monCases = new List<List<Integer>>();
        List<Integer> sList = new List<Integer>();
        Integer spiltCount =1;
        
        for(Integer i=0;i<caseList.size();i++) {
            sList.add(caseList[i]);
            if(i==CIList.size()-1) {
                monCases.add(sList);
                sList = new List<Integer>();
                spiltCount++;
            } else if(i == CIList.size()*spiltCount-1 || i==caseList.size()) {
                monCases.add(sList);
                sList = new List<Integer>();
                spiltCount++;
            }
        }
        
        /**
            每个sku 的case 装在 monCases 里面,
            eg: monCases[月数] [第几个sku]
            sku的名字装在 CIList 里面
            loop 完后 insert;
            加入inventory 里面的SKU的objs
        */
        for(Integer i=0;i<updateMonths.size();i++) {
             List<SKU__c> sk = [select id, name from SKU__c where MonthlyForecast__c=:updateMonths[i].id];
            for(Integer k=0;k<monCases[i].size();k++){
                   sk[k].Name = CIList[k].Name;
                   sk[k].Product__c =CIList[k].id;
                   sk[k].Rolling_Forcast__c = RollingForecastID;
                   sk[k].Case__c = monCases[i][k];
                   skuList.add(sk[k]);
            }
       }
       update skuList;
       
       /**
           将每个月的TotalRMB & toatalKG update 到已有的monthly forecast records
       */
       List<SKU__c> newList = [select id, name, MonthlyForecast__c, SumKG__c, SumRMB__c, KG__c, RMB__c from SKU__c];
       
       for(Integer i=0;i<updateMonths.size();i++){
            if(updateMonths[i].TotalRMB__c== null){
                 updateMonths[i].TotalRMB__c = 0.0;
             }
             if(updateMonths[i].TotalKG__c== null){
                 updateMonths[i].TotalKG__c = 0.0;
             }
           for(Integer j=0;j<newList.size();j++){
               if(updateMonths[i].id == newList [j].MonthlyForecast__c){

                   updateMonths[i].TotalKG__c += newList[j].SumKG__c; 
                   updateMonths[i].TotalRMB__c +=newList[j].SumRMB__c;
               }
           }
       }
       update updateMonths;
       
       /**
           update RollingForecast 
       */
       TotalForecast();
       
       RollingForecast__c r= [select r.Year__c, r.UserEmail__c, r.UniqueId__c, r.TotalForecastRMB__c, r.TotalForecastKG__c, r.Percentage__c,
               r.TotalBudgetRMB__c, r.TotalBudgetKG__c,  r.Sales_City__c, 
               r.OwnerId, r.Name, r.Month__c,  r.Id, 
               r.ForecastCity__c, r.Approver__c, r.ApprovalStatus__c, r.Apply_Date__c from RollingForecast__c r where r.id =: RollingForecastID limit 1][0];
       
       if(r.TotalForecastRMB__c>TotalBudgetRMB || r.TotalForecastKG__c>TotalBudgetKG){
           
           Database.rollback(sp);           
           initExistedRec();
       }else{
           if(percentage == null)
               percentage = String.valueOf(0);
          r.Percentage__c = Decimal.valueOf(percentage) ;
           upsert r;
           InsertOrUpdate= false;
           SaveVal ='修改';
       }
    }
    
     /**
        如果rolling forecast 没有在每个月的最后一天之前填完,
        那么之后就不能让填写那个月的forecast
    */
    public boolean isLastDay(){
        Date lastDay = Date.today().toStartOfMonth();
        //如果current date 小于 last day
        if(currentDate <= lastDay){
            return true;
        }
        else
            return false;
    }
   
    public boolean isDisable{get;set;}
    public void CloseWindow(){
        if(isLastDay()){
            isDisable = true;
            isShow = true;
            MsgShow = true;
            errMsg ='已经过了填写时间, 下个月趁早';
            MsgType ='WARNING';
        }
    }
    
    /**
        not a elegant way
        hard coding in solving the dynamic city inventory records
    */
    public void insertMonthsList() {
        //插入的时候, 判断 是否已经插入了的, 如果false, 则不能再插入
        if(!InsertOrUpdate){
            isShow = true;
            MsgType='WARNING';
            errMsg ='不能插入, 只能update';
            return;
        }
        insertMonths =new List<MonthlyForecast__c>();
        /**
            插入 monthly forecast 的记录
        */
        for(Integer i=0;i<CommonList.size();i++){
        
            Date dat = CurrentDate.addMonths(i+2);
            MonthlyForecast__c cde = new MonthlyForecast__c();
            cde.Rolling_Forcast__c = CommonList[i].RollingForecastID;
            cde.year__c = CommonList[i].Year;
            cde.month__c = CommonList[i].month;
            if(String.valueOf(cde.month__c).length()<2){
                    cde.Name = dat.year()+'年0'+dat.month()+'月预测';
            }else{
                cde.Name = dat.year()+'年'+dat.month()+'月预测';
            }          
            cde.Rate__c = CommonList[i].Rate;
            cde.City_Monthly_Budget__c = CommonList[i].budgetRMB;
            insertMonths.add(cde);
        }
        
       //Integer inCount=0;
        /**
            判断 如果 这个Rollingforecast 里面没有任何monthlyforecast的records,则 insert, 
            else edit
        */
  
        List<MonthlyForecast__c> lm = [select id, name from MonthlyForecast__c where Rolling_Forcast__c =: RollingForecastID];
        if(lm.size()<1){
            insert insertMonths;
        }
       
       /**
           将sku 的case 写入到 caseList list
       */
        List<SKU__c> skuList = new List<SKU__c>();
        List<Integer> caseList = new List<Integer>();
        
        for(Integer i=0;i<CommonList.size();i++){
            if(CIListCount>1)
                  caseList.add(CommonList[i].Resource1);
            if(CIListCount>2)
                  caseList.add(CommonList[i].Resource2);
            if(CIListCount>=3)
                  caseList.add(CommonList[i].Resource3);
            if(CIListCount>=4)
                  caseList.add(CommonList[i].Resource4);
            if(CIListCount>=5)
                  caseList.add(CommonList[i].Resource5);
            if(CIListCount>=6)
                  caseList.add(CommonList[i].Resource6);
            if(CIListCount>=7)
                  caseList.add(CommonList[i].Resource7);
        }
        
        /**
            首先创建一个 nested list 去装每个月的sku records,
            通过循环caseList, 每7个sku records装在一个list 里面,
            然后再装到 nested list
            
            这个nested list 为了就是要在下面的 insertion中, 
            一次性的 insert 所有的 skuList 到每个月的record 里面
        */
        List<List<Integer>> monCases = new List<List<Integer>>();
        List<Integer> sList = new List<Integer>();
        Integer spiltCount =1;
        
        for(Integer i=0;i<caseList.size();i++) {
            sList.add(caseList[i]);
            if(i==CIList.size()-1) {
                monCases.add(sList);
                sList = new List<Integer>();
                spiltCount++;
            } else if(i == CIList.size()*spiltCount-1 || i==caseList.size()) {
                monCases.add(sList);
                sList = new List<Integer>();
                spiltCount++;
            }
        }
        
        /**
            每个sku 的case 装在 monCases 里面,
            eg: monCases[月数] [第几个sku]
            sku的名字装在 CIList 里面
            loop 完后 insert;
            加入inventory 里面的SKU的objs
        */
         Integer monCount =1;
        for(Integer i=0;i<insertMonths.size();i++) {
            for(Integer k=0;k<monCases[i].size();k++){
                   SKU__c sku = new SKU__c();
                   sku.Name = CIList[k].name;
                   sku.Product__c =CIList[k].id;
                   sku.MonthlyForecast__c = insertMonths[i].id;
                   sku.Rolling_Forcast__c = RollingForecastID;
                   sku.Case__c = monCases[i][k];
                   skuList.add(sku);
            }
       }
       
       /**
           如果 monthly records insert 成功, 则insert skuList
       */
       if(InsertOrUpdate!=null && InsertOrUpdate){ 
           
           insert skuList;
       }else if(InsertOrUpdate!=null && !InsertOrUpdate){
           upsert skuList;
       }else{
           insert skuList;
       }
      
       /**
           将每个月的TotalRMB & toatalKG update 到已有的monthly forecast records
       */
       List<SKU__c> newList = [select id, name, MonthlyForecast__c, SumKG__c, SumRMB__c, KG__c, RMB__c from SKU__c];
       
       for(Integer i=0;i<insertMonths.size();i++){
            if(insertMonths[i].TotalRMB__c== null){
                 insertMonths[i].TotalRMB__c = 0.0;
             }
             if(insertMonths[i].TotalKG__c== null){
                 insertMonths[i].TotalKG__c = 0.0;
             }
           for(Integer j=0;j<newList.size();j++){
               if(insertMonths[i].id == newList [j].MonthlyForecast__c){
                   insertMonths[i].TotalKG__c += newList[j].SumKG__c; 
                   insertMonths[i].TotalRMB__c +=newList[j].SumRMB__c;
               }
           }
       }
       update insertMonths;
       
       /**
           如果forecast的东西大于 budget的
           DELETE it
       */
       TotalForecast();
       RollingForecast__c r= [select TotalForecastRMB__c, TotalForecastKG__c,TotalBudgetRMB__c,TotalBudgetKG__c, Percentage__c
                                               from RollingForecast__c where id =: RollingForecastID limit 1][0];
                                               
       if(r.TotalForecastRMB__c>r.TotalBudgetRMB__c || r.TotalForecastKG__c>r.TotalBudgetKG__c){
           
           DELETE insertMonths;
            initExistedRec();
       }else{
           if(percentage == null)
               percentage = String.valueOf(0);
          r.Percentage__c =Decimal.valueOf(percentage);
           upsert r;
           InsertOrUpdate= false;
           SaveVal ='修改';
       }
    }
    
    public PageReference Approvals(){
        
        RollingForecast__c r= [select id,ForecastCity__c,UserEmail__c,NMKTDirector__r.email,NMMM__r.email,NMSalesDirector__r.email,RMMM__r.email, RSM__r.email,SRMMM__r.email, ApprovalStatus__c from RollingForecast__c where id =: RollingForecastID limit 1][0];
        
        if(cu.ProfileId == ProfileRMMM.id)
        {
            r.ApprovalStatus__c = 'Waiting for RSM Approve';
            List<String> toAddresses = new List<String>();
            toAddresses.add( r.RSM__r.email );  
            List<String> ccAddresses = new List<String>();
            String Body = '您好！\n' + '您收到一个新的'+ r.ForecastCity__c +'滚动预测审批申请!\n' + string.valueof(Date.Today());
            String Subject = r.ForecastCity__c + '滚动预测审批申请';
            
            if(toAddresses.size()>0 ){
                EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);
            }
            
            DisplayReject =false;
            update r;
            pageReference pr = new pageReference('/a1q/o');
            pr.setredirect(true);
            return pr;
        }

        if((cu.ProfileId == ProfileRSM.id)||(cu.ProfileId == ProfileRSM_TM.id))
        {
            if(insertOrUpdate){
                insertMonthsList();
            }
            initExistedRec();
            
            if(!insertOrUpdate){
                updateMonthsList();
            }
            initExistedRec();
            displayRecords();
        
            r.ApprovalStatus__c = 'Waiting for SRMMM Approve';
            List<String> toAddresses = new List<String>();
            toAddresses.add( r.SRMMM__r.email );
            List<String> ccAddresses = new List<String>();
            String Body = '您好！\n' + '您收到一个新的'+ r.ForecastCity__c +'滚动预测审批申请!\n' + string.valueof(Date.Today());
            String Subject = r.ForecastCity__c + '滚动预测审批申请';
            if(toAddresses.size()>0 ){
                EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);
            }
           
            update r;
            pageReference pr = new pageReference('/a1q/o');
            pr.setredirect(true);
            return pr;
        }

        if(cu.ProfileId == ProfileSRMMM.id)
        {
            r.ApprovalStatus__c = 'Waiting for National Medical MKT Manager Approve';  
            List<String> toAddresses = new List<String>();
            toAddresses.add( r.NMMM__r.email );  
            List<String> ccAddresses = new List<String>();
            String Body = '您好！\n' +  '您收到一个新的'+ r.ForecastCity__c +'滚动预测审批申请!\n' + string.valueof(Date.Today());
            String Subject = r.ForecastCity__c + '滚动预测审批申请';
            
            if(toAddresses.size()>0 ){
                EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);
            }
            
            update r;
            pageReference pr = new pageReference('/a1q/o');
            pr.setredirect(true);
            return pr;
        }
        
        if(cu.UserRoleId == RoleNMMKTM.id)
        {
            if(insertOrUpdate){
                insertMonthsList();
            }
            initExistedRec();
            
            if(!insertOrUpdate){
                updateMonthsList();
            }
            initExistedRec();
            displayRecords();
        
            r.ApprovalStatus__c = 'Waiting for Medical MKT Director Approve'; 
            List<String> toAddresses = new List<String>();
            toAddresses.add( r.NMKTDirector__r.email );  
            List<String> ccAddresses = new List<String>();
            String Body = '您好！\n' +  '您收到一个新的'+ r.ForecastCity__c +'滚动预测审批申请!\n' + string.valueof(Date.Today());
            String Subject = r.ForecastCity__c + '滚动预测审批申请';
            if(toAddresses.size()>0 ){
                EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);
            }
            
            update r;
            pageReference pr = new pageReference('/a1q/o');
            pr.setredirect(true);
            return pr;                                            
        }

        if(cu.UserRoleId == RoleNMKD.id)
        {
            if(insertOrUpdate){
                insertMonthsList();
            }
            initExistedRec();
            
            if(!insertOrUpdate){
                updateMonthsList();
            }
            initExistedRec();
            displayRecords();
            
            r.ApprovalStatus__c = 'Waiting for National Medical Sales Director Approve'; 
            List<String> toAddresses = new List<String>();
            toAddresses.add( r.NMSalesDirector__r.email );  
            List<String> ccAddresses = new List<String>();
            String Body = '您好！\n' +  '您收到一个新的'+ r.ForecastCity__c +'滚动预测审批申请!\n' + string.valueof(Date.Today());
            String Subject = r.ForecastCity__c + '滚动预测审批申请';
             if(toAddresses.size()>0 ){
                EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);
            }
             
             update r; 
             pageReference pr = new pageReference('/a1q/o');
             pr.setredirect(true);
             return pr;                                          
        }

        if(cu.UserRoleId == RoleNMSD.id)
        {
            if(insertOrUpdate){
                insertMonthsList();
            }
            initExistedRec();
            
            if(!insertOrUpdate){
                updateMonthsList();
            }
            initExistedRec();
            displayRecords();
            
            r.ApprovalStatus__c = 'Approve'; 
            List<String> toAddresses = new List<String>();
            toAddresses.add( r.UserEmail__c );  
            List<String> ccAddresses = new List<String>();
            String Body = '您好！\n' + '您提交的'+ r.ForecastCity__c +'滚动预测申请审批通过!\n' + string.valueof(Date.Today());
            String Subject = r.ForecastCity__c + '滚动预测申请审批通过';
            if(toAddresses.size()>0 ){
                EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);
            }
            
            update r;
            pageReference pr = new pageReference('/a1q/o');
            pr.setredirect(true);
            return pr;                                        
        }
        return null;
    }
        
        public PageReference Reject(){
        
        RollingForecast__c r= [select id,UserEmail__c, ForecastCity__c, ApprovalStatus__c from RollingForecast__c where id =: RollingForecastID limit 1][0];
        r.ApprovalStatus__c = 'Reject';
        update r;

        List<String> toAddresses = new List<String>();
        toAddresses.add( r.UserEmail__c );  
        List<String> ccAddresses = new List<String>();
        String Body = '您好！\n' + '您提交的'+ r.ForecastCity__c +'滚动预测申请被拒绝!\n' + string.valueof(Date.Today());
        String Subject = r.ForecastCity__c + '滚动预测申请被拒绝';

        if(toAddresses.size()>0 ){
                EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);
         }
         pageReference pr = new pageReference('/a1q/o');
         pr.setredirect(true);
         return pr;
    }

    public PageReference save(){
        if(insertOrUpdate){
            insertMonthsList();
        }
        //initExistedRec();
        if(!insertOrUpdate){
            updateMonthsList();
        }
        initExistedRec();
        displayRecords();
        return null;
    }    

    /**
        审批后, 这条记录将update approvalstatus
    */
    public PageReference Submit(){
        
        if(insertOrUpdate){
            insertMonthsList();
        }
        initExistedRec();
        
        if(!insertOrUpdate){
            updateMonthsList();
        }
        initExistedRec();
        displayRecords();
        
        r.ApprovalStatus__c = 'Waiting for RMMM Approve';
        r.UserEmail__c = cu.email;  
        r.RSM__c = cu.ManagerId;
        List<Id> UserIds = new List<Id>();

       User Srmmm = [Select ManagerId From User Where id =: r.RMMM__c]; 
       UserIds.add(r.RMMM__c);
       r.SRMMM__c = Srmmm.ManagerId;
       UserIds.add(r.SRMMM__c);
       
       User Nmmm = [Select id From User Where UserRoleId =: RoleNMMKTM.id];
       r.NMMM__c = Nmmm.id;
       UserIds.add(r.Nmmm__c);

       User Nmkd = [Select id From User Where UserRoleId =: RoleNMKD.id and IsActive =: true];

       r.NMKTDirector__c = Nmkd.id;
       UserIds.add(r.NMKTDirector__c);

       User Nmsd = [Select id From User Where UserRoleId =: RoleNMSD.id];
       r.NMSalesDirector__c = Nmsd.id;
       UserIds.add(r.NMSalesDirector__c);
        List<User> Users = [Select u.id, email, u.UserRoleId, u.ProfileId From User u where u.id in: UserIds];
      
        for(User u : Users)
        {
            if(u.ProfileId == ProfileRMMM.id)
            {
                r.RMMM__c = u.id;
                List<String> toAddresses = new List<String>();
                toAddresses.add( u.email );  
                List<String> ccAddresses = new List<String>();
                String Body = '您好！\n' + '您收到一个新的'+ r.ForecastCity__c +'滚动预测审批申请!\n' + string.valueof(Date.Today());
                String Subject = r.ForecastCity__c + '滚动预测审批申请';
                if(toAddresses.size()>0 ){
                     EmailFunction.SendEmail(toAddresses, ccAddresses, Body, Subject);             
                }
            }

            if(u.ProfileId == ProfileSRMMM.id)
            {
                r.SRMMM__c = u.id;       
            }
    
            if(u.ProfileId == ProfileNMMM.id)
            {
                r.NMMM__c = u.id;          
            }
    
            if(u.UserRoleId == RoleNMKD.id)
            {
                r.NMKTDirector__c = u.id;             
            }
    
            if(u.UserRoleId == RoleNMSD.id)
            {
                r.NMSalesDirector__c = u.id;   
            }            
        }
        update r;
        pageReference pr = new pageReference('/a1q/o');
        pr.setredirect(true);
        return pr;
    }
    public PageReference Cancel(){
        pageReference pr = new pageReference('/a1q/o');
        pr.setredirect(true);
        return pr;
    }

    public PageReference actionSupport(){
        return null;
    }
}